//
//  DianPuViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/23.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "DianPuViewController.h"

#import "DPDetView.h"
#import "DPDetCell.h"
#import "BuyCarDetViewController.h"
@interface DianPuViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSMutableArray *datas;
}

@property (nonatomic,strong)UITableView *tableView;

@end

@implementation DianPuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setBGColor];
    datas = [NSMutableArray array];
    [self showNav:@"店铺详情"];
    
    [self loadData];
    
}

- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    DPDetView *view = [[[NSBundle mainBundle] loadNibNamed:@"DPDetView" owner:nil options:nil] lastObject];
    view.frame = CGRectMake(0, 0, ScreenWidth, 260.0);
    _tableView.tableHeaderView = view;
    
    view.labName.text = _name;
    view.labPlace.text = _place;
    view.labCount.text = _count;
    view.labKm.text = _km;

    UIButton *btnBackTop = [UIButton buttonWithType:UIButtonTypeCustom];
    btnBackTop.frame = CGRectMake(ScreenWidth-16-49, ScreenHeight-49-20, 49, 49);
    [btnBackTop setImage:[UIImage imageNamed:@"返回顶部"] forState:UIControlStateNormal];
    [btnBackTop setImage:[UIImage imageNamed:@"返回顶部"] forState:UIControlStateHighlighted];
    [btnBackTop addTarget:self action:@selector(clickTop) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnBackTop];
    
}

- (void)clickTop{
    [UIView animateWithDuration:0.3 animations:^{
        [_tableView setContentOffset:CGPointMake(0, 0) animated:YES];
    }];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return datas.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    DPDetCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"DPDetCell" owner:nil options:nil] lastObject];
    }
    NSDictionary *dic = datas[indexPath.row];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.lab2.layer.borderColor = COLOR_RGB(17, 100, 253, 1).CGColor;
    cell.lab2.layer.cornerRadius = 5.0;
    cell.lab2.layer.borderWidth = 1.0;
    
    cell.lab1.text = [dic objectForKey:@"brand"];
    NSString *type = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"type"] integerValue]];
    if ([type isEqualToString:@"1"]) {
        cell.lab2.text = @"准新车";
    }else{
        cell.lab2.text = @"新车";
    }
    NSString *guideprice = [NSString stringWithFormat:@"%@万",[dic objectForKey:@"sellingprice"]];
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:guideprice attributes:attribtDic];
    
    cell.lab3.attributedText = attribtStr;
    cell.lab4.text = [NSString stringWithFormat:@"%@万",[dic objectForKey:@"guideprice"]];
    NSString *count = [NSString stringWithFormat:@" %li",[[dic objectForKey:@"see"] integerValue]];
    [cell.btnLL setTitle:count forState:UIControlStateNormal];
    
    NSString *imgurl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[dic objectForKey:@"carthemephoto"]];
    [cell.images sd_setImageWithURL:[NSURL URLWithString:imgurl] placeholderImage:[UIImage imageNamed:@"timg22"]];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 116.0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = datas[indexPath.row];
    NSString *carid = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"id"] integerValue]];
    BuyCarDetViewController *det = [[BuyCarDetViewController alloc] init];
    det.carId = carid;
    det.lat = _lat;
    det.lng = _lng;
    [self.navigationController pushViewController:det animated:YES];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 39, ScreenWidth, 0.5)];
    lab.backgroundColor = [UIColor grayColor];
    [view addSubview:lab];
    
    NSArray *arr = @[@"综合",@"价格",@"车型",@"排量"];
    for (NSInteger i = 0; i<arr.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(ScreenWidth/4*i, 0, ScreenWidth/4, 40);
        [btn setTitle:arr[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"多边形-2"] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:15.0];
        //设置文字在左，图片在右
        [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, -btn.imageView.image.size.width, 0, btn.imageView.image.size.width)];
        [btn setImageEdgeInsets:UIEdgeInsetsMake(0, btn.titleLabel.frame.size.width+5, 0, -btn.titleLabel.frame.size.width)];
        
        btn.tag = i;
        [btn addTarget:self action:@selector(clicks:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:btn];
        
    }
    
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40.0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40.0)];
    
    return view;
}

- (void)clicks:(UIButton *)btn{
    
}
//店铺详情
- (void)loadData{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:_lng forKey:@"longitude"];
    [parmas setObject:_lat forKey:@"latitude"];
    [parmas setObject:_CarId forKey:@"id"];
    NSLog(@"parmas:%@",parmas);
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:@"http://ydd.xtangguo.com/index.php/App/CarBuying/carbuying" parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"res:%@",responseObject);
        datas = [responseObject objectForKey:@"result"];
        [_tableView reloadData];
        //
        [self loadUI];
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
